package topo.dataview;

import com.l2fprod.common.DefaultSheetPanel;
import com.zhtelecom.common.snmp.SNMPAPI;
import com.zhtelecom.common.snmp.SNMPFactory;
import com.zhtelecom.common.snmp.SNMPTarget;
import java.awt.Container;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.BeanInfo;
import java.util.ResourceBundle;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import topo.table.ObjectTableModel;
import topo.utils.DialogTools;

public class SNMPSheetDialog extends JDialog {
    public static ResourceBundle res = ResourceBundle.getBundle("topo.dataview.DefaultSheetSNMPDialogRes");
    JPanel jPanel2 = new JPanel();
    public JButton jButtonFresh = new JButton();
    public JButton jButtonAdd = new JButton();
    public JButton jButtonUpdate = new JButton();
    public JButton jButtonClose = new JButton();
    JLabel jLabel1 = new JLabel();
    JLabel jLabel2 = new JLabel();
    public DefaultSheetPanel sheetPanel = null;

    private boolean isAdd = false;
    private boolean isUpdate = false;
    boolean showFresh;
    public ObjectTableModel mod;
    BeanInfo beanInfo;
    SNMPTarget target;

    public Object getData() {
        return this.sheetPanel.getData();
    }

    public String dataInputCheck(boolean isAdd) {
        return null;
    }

    protected void initObject(SNMPTarget target, Object obj, ObjectTableModel source) {
        this.mod = source;
        initObject(target, obj);
    }

    public void initObject(SNMPTarget target, Object obj) {
        this.target = target;
        this.sheetPanel.resetObject(obj);
    }

    void init(boolean isAdd, boolean isUpdate, boolean showFresh, BeanInfo beanInfo) {
        this.isAdd = isAdd;
        this.beanInfo = beanInfo;
        this.showFresh = showFresh;
        this.isUpdate = isUpdate;
        try {
            jbInit();
            this.sheetPanel = new DefaultSheetPanel(beanInfo, null);
            getContentPane().add(this.sheetPanel, "Center");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public SNMPSheetDialog(Frame owner, boolean isAdd, boolean isUpdate, boolean showFresh, BeanInfo beanInfo) {
        super(owner, true);
        init(isAdd, isUpdate, showFresh, beanInfo);
    }

    public SNMPSheetDialog(Dialog owner, boolean isAdd, boolean isUpdate, boolean showFresh, BeanInfo beanInfo) {
        super(owner, true);
        init(isAdd, isUpdate, showFresh, beanInfo);
    }

    private void jbInit() throws Exception {
        ActionListener addAction = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                SNMPSheetDialog.this.processAdd();
            }
        };
        ActionListener updateAction = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                SNMPSheetDialog.this.processUpdate();
            }
        };
        ActionListener freshAction = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                SNMPSheetDialog.this.processFresh();
            }
        };
        ActionListener closeAction = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                SNMPSheetDialog.this.processClose();
            }
        };
        this.jButtonFresh.setText(res.getString("String_2"));
        this.jButtonFresh.addActionListener(freshAction);
        this.jButtonUpdate.setText(res.getString("String_3"));
        this.jButtonUpdate.addActionListener(updateAction);
        this.jButtonClose.setText(res.getString("String_4"));
        this.jButtonClose.addActionListener(closeAction);
        this.jLabel1.setText("        ");
        this.jLabel2.setText("        ");

        this.jButtonAdd.setText(res.getString("String_7"));
        this.jButtonAdd.addActionListener(addAction);
        getContentPane().add(this.jPanel2, "South");
        if (this.isAdd) {
            this.jPanel2.add(this.jButtonAdd, null);
        }
        if (this.isUpdate) {
            this.jPanel2.add(this.jButtonUpdate, null);
        }
        this.jPanel2.add(this.jLabel1, null);
        if (this.showFresh) {
            this.jPanel2.add(this.jButtonFresh, null);
        }
        this.jPanel2.add(this.jLabel2, null);
        this.jPanel2.add(this.jButtonClose, null);
        setDefaultCloseOperation(2);
    }

    public void processFresh() {
        try {
            SNMPAPI snmpService = SNMPFactory.getSNMPAPI();
            Object mibObj = this.sheetPanel.getData();
            Object newmibobj = snmpService.getMibObject(mibObj, this.target);

            this.sheetPanel.resetObject(newmibobj);
            if (this.mod != null) {
                this.mod.updateRowObject(mibObj);
            }
        } catch (Exception ex) {
            DialogTools.showErroMsg(this, res.getString("String_1"));
            ex.printStackTrace();
        }
    }

    public void processUpdate() {
        this.sheetPanel.writeToObject();
        String msg = dataInputCheck(false);
        if ((msg != null) && (msg.length() > 0)) {
            DialogTools.showErroMsg(this, res.getString("String_8") + msg);
            return;
        }
        try {
            SNMPAPI snmpService = SNMPFactory.getSNMPAPI();
            Object mibObj = this.sheetPanel.getData();
            snmpService.update(mibObj, this.target);
            if (this.mod != null) {
                this.mod.updateRowObject(mibObj);
            }
            DialogTools.showOkMsg(this, res.getString("String_9"), res.getString("String_10"));
        } catch (Exception ex) {
            DialogTools.showErroMsg(this, res.getString("String_11"));
            ex.printStackTrace();
        }
    }

    public void processClose() {
        dispose();
    }

    public void processAdd() {
        this.sheetPanel.writeToObject();
        String msg = dataInputCheck(true);
        if ((msg != null) && (msg.length() > 0)) {
            DialogTools.showErroMsg(this, res.getString("String_12") + msg);
            return;
        }
        try {
            SNMPAPI snmpService = SNMPFactory.getSNMPAPI();
            Object mibObj = this.sheetPanel.getData();
            snmpService.addTableRow(mibObj, this.target);
            if (this.mod != null) {
                this.mod.addRowObject(mibObj);
            }
            DialogTools.showOkMsg(this, res.getString("String_13"), res.getString("String_14"));
            dispose();
        } catch (Exception ex) {
            DialogTools.showErroMsg(this, res.getString("String_15"));
            ex.printStackTrace();
        }
    }
}